<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWorkOrder extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('work_order')) {
            return;
        }
        Schema::create('work_order', function (Blueprint $table) {
            $table->id();
            $table->string('wo_number')->comment('工单编号');
            $table->string('type')->comment('工单类型');
            $table->string('status')->comment('状态');
            $table->string('service_type')->comment('服务类型');
            $table->string('service_content')->comment('服务内容');
            $table->string('plan_at')->comment('计划时间');
            $table->text('description')->comment('描述');
            $table->string('level')->comment('优先级');
            $table->string('contact_name')->comment('联系人');
            $table->string('contact_phone')->comment('联系电话');
            $table->unsignedInteger('country_id');
            $table->unsignedInteger('province_id');
            $table->unsignedInteger('city_id');
            $table->unsignedInteger('county_id');
            $table->string('country_name')->comment('国家');
            $table->string('province_name')->comment('省份');
            $table->string('city_name')->comment('城市');
            $table->string('county_name')->comment('区县');
            $table->string('address')->comment('详细地址');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        Schema::create('work_order_product', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_id');
            $table->unsignedInteger('product_id');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        Schema::create('work_order_images', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_id');
            $table->string('image');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
        // 回执
        Schema::create('work_order_receipt', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_id');
            $table->string('reason')->comment('故障原因');
            $table->string('process_type')->comment('处理方式');
            $table->string('signature')->comment('客户签名URL');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
        Schema::create('work_order_receipt_image', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_receipt_id');
            $table->string('image');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
        // 配件
        Schema::create('work_order_receipt_bom', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_receipt_id');
            $table->unsignedInteger('bom_id');
            $table->unsignedInteger('count');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
        // 服务项目
        Schema::create('work_order_receipt_service', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_receipt_id');
            $table->string('name')->comment('项目名称');
            $table->string('unit')->comment('单位');
            $table->unsignedInteger('count')->comment('数量');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
        // 客户反馈
        Schema::create('work_order_receipt_feedback', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_receipt_id');
            $table->unsignedInteger('satisfaction')->comment('客户满意度');
            $table->unsignedInteger('attitude')->comment('服务态度');
            $table->unsignedInteger('efficiency')->comment('效率');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
        // 状态变更历史
        Schema::create('work_order_status_history', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('work_order_id');
            $table->string('status')->comment('状态');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('work_order');
    }
}
